<?php
/* 
 * メイル送信オブジェクト （添付あり）
 * 
 * author	；t.odaka
 * date		;2005/10/13
 * 
 * modified;	2009/3/14	t.odaka
 * 				2009/5/22	t.odaka	デバッグログの削除
*/

require_once("../PEAR/Mail.php");
require_once("../PEAR/Mail/mime.php");
require_once('../Myznala/Config.php');

class SendMailBean {
	/* プロパティー */
	var $_Smtphost, 
		$Smtpport, 
		$Smtpauth, 
		$_To,
		$_Cc,
		$_Bcc,
		$_From, 
		$_Subject,
		$_Message,
		$_Attach;
	
	/* デフォルトのコンストラクタ */
	function __construct() {
		
		require("../Myznala/debugLog.php");
		// debugログの出力
//		$myDebugLog->debug("constructor: start");
		
		$this->setSmtphost();
		$this->setSmtpport();
		$this->setSmtpauth();
		$this->setFrom();
		
		// debugログの出力
//		$myDebugLog->debug("constructor: end");
	}

	/* getter */
	function getSmtphost(){return $this->_Smtphost;}
	function getSmtpport(){return $this->_Smtpport;}
	function getSmtpauth(){return $this->_Smtpauth;}
	function getTo(){return $this->_To;}
	function getCc(){return $this->_Cc;}
	function getBcc(){return $this->_Bcc;}
	function getFrom(){return $this->_From;}
	function getSubject(){return $this->_Subject;}
	function getMessage(){return $this->_Message;}
	function getAttach(){return $this->_Attach;}

	/* setter */
	function setSmtphost(){$this->_Smtphost=SMTPHOST;}
	function setSmtpport(){$this->_Smtpport=SMTPPORT;}
	function setSmtpauth(){$this->_Smtpauth=SMTPAUTH;}
	function setFrom(){$this->_From=MAILFROM;}
	function setTo($arg){$this->_To=$arg;}
	function setCc($arg){$this->_Cc=$arg;}
	function setBcc($arg){$this->_Bcc=$arg;}
	function setSubject($arg){
		//jis(iso-2022-jp後、base64に変換)
//		$wk = mb_convert_encoding($arg, "ISO-2022-JP", "auto");
//		$wk = base64_encode($wk);
//		$wk = '=?ISO-2022-JP?B?'.$wk;
//		$wk = '=?ISO-2022-JP?B?'.$wk.'?=';
		$this->_Subject=$arg;
	}
	function setMessage($arg){$this->_Message=$arg;}
	function setAttach($arg){$this->_Attach=$arg;}

	function mailsend(){
		require("../Myznala/debugLog.php");
		// debguログの出力
//		$myDebugLog->debug("mailsend: start");
		
		$mail_array = array(
			'host'	=>	$this->_Smtphost,
			'port'	=>	$this->_Smtpport,
			'auth'	=>	$this->_Smtpauth
		);
		
		// Mail　オブジェクトの作成		
		$mail_obj	=&	Mail::factory('smtp',$mail_array);
		
		// debugログの出力
//		$myDebugLog->debug("host: ".$this->_Smtphost);
//		$myDebugLog->debug("port: ".$this->_Smtpport);
//		$myDebugLog->debug("auth: ".$this->_Smtpauth);
//		$myDebugLog->debug("mail_obj: created.");
		
		$headers=array();
		$headers["From"]=$this->_From;
		$headers["To"]=$this->_To.'<'.$this->_To.'>';
		$headers["Cc"]=$this->_Cc.'<'.$this->_Cc.'>';
		$headers["Bcc"]=$this->_Bcc.'<'.$this->_Bcc.'>';
		$headers["Subject"]
			=mb_convert_encoding($this->_Subject,'ISO-2022-JP','auto');
//		$headers["Subject"]=$this->_Subject;
		
		$recipient = $this->_To;

		// debugログの出力
//		$myDebugLog->debug("recipient: ".$recipient);
		
		$body=mb_convert_encoding($this->_Message,'ISO-2022-JP','auto');
		// debugログの出力
//		$myDebugLog->debug("message: ".$this->_Message);
		
		// Mail_Mime　オブジェクトの作成		
		$o_mime=new Mail_Mime("\n");
		
		// debugログの出力
//		$myDebugLog->debug("o_mime: created.");
		
		$o_mime->setTxtBody($body);
		
		if($this->_Attach!=null){
			$o_mime->addAttachment($this->_Attach);
			// debugログの出力
//			$myDebugLog->debug("o_mime: file attached.");
		}

		$build_param=array(
			"head_charset" => "iso-2022-jp",
			"text_charset" => "iso-2022-jp"
		);
		
		$body = $o_mime->get($build_param);
		$headers = $o_mime->headers($headers);
		
		// debugログの出力
//		$myDebugLog->debug("mailsend() prepared");
		
		$ret = $mail_obj->send($recipient,$headers,$body);

		// debugログの出力
//		$myDebugLog->debug("mailsend() end ; return ".$ret);
		return;
	}
}
?>